package opennlp.wrappers;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.util.Span;
import opennlp.wrappers.OpenNLPModelsManager.LANGUAGE;
import opennlp.wrappers.OpenNLPModelsManager.MODEL_TYPE;

/**
 *
 * @author Luis A Riveros
 */
public class NameFinderW {

    private NameFinderME nameFinder;
    private OpenNLPModelsManager.LANGUAGE language;
    private OpenNLPModelsManager.MODEL_TYPE model_type;

    public NameFinderW(OpenNLPModelsManager.LANGUAGE language, OpenNLPModelsManager.MODEL_TYPE model_type) {
        this.language = language;
        this.model_type = model_type;
        OpenNLPModelsManager modelsManager = OpenNLPModelsManager.getInstance();
        try {
            InputStream inputStream = modelsManager.loadModel(model_type, language);
            TokenNameFinderModel nameFinderModel = new TokenNameFinderModel(inputStream);
            nameFinder = new NameFinderME(nameFinderModel);
        } catch (FileNotFoundException fnfe) {
            Logger.getLogger(SentenceDetectorW.class.getName()).log(Level.SEVERE, "Model file not found", fnfe);
        } catch (IOException ioe) {
            Logger.getLogger(SentenceDetectorW.class.getName()).log(Level.SEVERE, "Error loading model", ioe);
        }
    }

    public LANGUAGE getLanguage() {
        return language;
    }

    public MODEL_TYPE getModel_type() {
        return model_type;
    }

    public Span[] find(String[] sentence) {
        return nameFinder.find(sentence);
    }

    public Span[] find(String[] tokens, String[][] additionalContext) {
        return nameFinder.find(tokens, additionalContext);
    }

    public void clearAdaptiveData() {
        nameFinder.clearAdaptiveData();
    }
}
